const {_loginServer,_getlistlength,_getlistServer,_deleteServer,_editServer,_addServer,_batchdeletionServer,_SelectServer,_getUsernameServer,_updatePasswordServer} = require('../service/index')
const jwt = require('jsonwebtoken')
const nodemailer = require('nodemailer'); 
let codes = null;
module.exports = {
    // 登录接口
    async _login(ctx){
        const {username,password} = ctx.request.body;
        const result = await _loginServer({
            username,
            password
        })
        console.log(result);
        if(result.length){
            const token = jwt.sign({
            data: 'foobar'
            }, '2107cld', { expiresIn: 60 * 60 * 1000 * 2 });
            ctx.body={
                code:1,
                message:'登录成功',
                token
            }
        } else {
             ctx.body={
                code:0,
                message:'登录失败,请检查账号密码'
            }
        }
    },
    // 发送验证码的接口
    async _sendCode(ctx){
        const {username} = ctx.request.body;
        console.log(username);
        const result = await _getUsernameServer(username)
        if(result.length){
            // 生成随机验证码  
            function generateVerificationCode() {  
                return Math.random().toString(36).substr(2, 6);  
            }
            // 配置SMTP服务器 
            const transporter = nodemailer.createTransport({  
                service: 'qq', // 使用QQ邮箱的SMTP服务器  
                auth: {  
                    user: '1900039072@qq.com', // 你的QQ邮箱地址  
                    pass: 'ceerkumicyzabihi' // 你的QQ邮箱SMTP授权码  
                }  
            });  
            // 发送邮件  
            function sendVerificationEmail(toEmail, code) {  
                const mailOptions = {  
                    from: '1900039072@qq.com', // 发件人地址  
                    to: toEmail, // 收件人地址  
                    subject: '验证码', // 邮件主题  
                    text: `你的验证码是：${code}` // 邮件正文  
            };  
            
                transporter.sendMail(mailOptions, function(error, info){  
                    if (error) {  
                        console.log(error);  
                    } else {  
                        console.log('Email sent: ' + info.response);  
                    }  
                });  
            }  
            
            const toEmail = '1900039072@qq.com'; // 收件人的QQ邮箱地址  
            const code = generateVerificationCode(); // 生成验证码  
            sendVerificationEmail(toEmail, code); // 发送邮件
            codes = code;
            ctx.body={  
                code:1,
                message:'验证码发送成功'
            }
            
        } else {
            ctx.body={
                code:0,
                message:'该账号不存在,请检查账号'
            }
        }
    },
    // 修改密码接口
    async _updatePassword(ctx){
        const {username,code,password} = ctx.request.body;
        if(code === codes){
            const result = await _updatePasswordServer({
            username,
            code,
            password
        })

        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'密码修改成功'
            }
        } else {
            ctx.body={
                code:0,
                message:''
            }
        }

        } else {
            ctx.body={
                code:0,
                message:'验证码错误'
            }
        }
        
        
    },
    // 获取列表接口
    async _getlist(ctx){
        const {page,size} = ctx.query;
        console.log(page,size,'123123123123123');
        const resultLength = await _getlistlength()
        const result = await _getlistServer({
            page,size
        })
        // console.log(result);
        if(result.length){
            ctx.body={
                code:1,
                message:'获取成功',
                Alllist:resultLength,
                lengths:resultLength.length,
                result
            }
        } else {
             ctx.body={
                code:0,
                message:'获取失败',
               
            }
        }
    },
    // 删除接口
    async _delete(ctx){
        const {id} = ctx.query;
        console.log(id,'123123');
        const result = await _deleteServer({
            id
        })
        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'删除成功'
        }
    } else {
             ctx.body={
                code:0,
                message:'删除失败'}
            }
    },
    // 根据id修改
    async _update(ctx){
    const {stuNo,stuName,status,salary,workCity,collegeId,expertiseId,origin,className,createdTime,lasttModTime,archived,gradYear,id} = ctx.request.body;
    const result = await _editServer({
            stuNo,stuName,status,salary,workCity,collegeId,expertiseId,origin,className,createdTime,lasttModTime,archived,gradYear,id
    })
    if(result.affectedRows){
        ctx.body={
            code:1,
            message:'修改成功'
        }
    } else {
        ctx.body={
            code:0,
            message:'修改失败'
        }
    }
    },
    // 添加
    async _addlist(ctx){
        const {stuNo,stuName,status,salary,workCity,collegeId,expertiseId,origin,className,createdTime,lastModTime,archived,gradYear} = ctx.request.body;
        const result = await _addServer({
            stuNo,stuName,status,salary,workCity,collegeId,expertiseId,origin,className,createdTime,lastModTime,archived,gradYear
        })
        console.log(result);
        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'添加成功'
            }
        } else {
            ctx.body={
                code:0,
                message:'添加失败'
            }
        }
    },
    // 批量删除
    async _batchdeletion(ctx){
        const ids = ctx.request.body;
        console.log(ids);
        const result = await _batchdeletionServer({
            ids
        })
        console.log(result);
        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'批量删除成功'
            }
        } else {
            ctx.body={
                code:0,
                message:'删除失败'
            }
        }
    },
    // 查询
    async _getSelectList (ctx){
       const { name,market,scholl,address} = ctx.query
       const result = await _SelectServer(name,market,scholl,address)
       console.log(result);
       if(result.length){
        ctx.body={
            code:1,
            message:'查询成功',
            result
        }
       } else {
           ctx.body={
            code:0,
            message:'查询失败'
           }
       }
    }
}